/**
 * Created by asus on 2018/9/19.
 */
define(['jquery', 'template', 'cookie'], function ($, template) {
  var docMens = [{
    name: '病历管理',
    href: "doctor/doctor-detail.html"
  }, {
    name: '个人资料',
    href: "doctor/doctor-detail.html"
  }, {
    name: '账号设置',
    href: "doctor/update-password.html"
  }]
  var medicineRepMenus = [{
    name: '我的医生',
    href: "11"
  }, {
    name: '病历管理',
    href: "111"
  }, {
    name: '个人资料',
    href: ""
  }, {
    name: '账号设置',
    href: ""
  }]


  // 获取菜单
  getMenu()

  // 获取并渲染优秀病例模板
  getRecords()


  // 点击菜单 显示菜单栏
  $("#role-menu").on('click', '.user-menu', function () {
    var e = window.event || event;
    if (e.stopPropagation) {
      e.stopPropagation();
    } else {
      e.cancelBubble = true;
    }
    $(this).children('.menu-list').slideDown()
  })
  document.onclick = function () {
    $("#role-menu .menu-list").slideUp();
  };
  // 退出登录
  $("#loginout-btn").on('click', function () {
    $.cookie('userToken', null)
    //$.ajax({
    //  type: 'GET',
    //  url: '/loginout',
    //  success: function (res) {
    //    if (res.code == 200) {
    //
    //    }
    //  }
    //
    //})
  })
  // 首页 tab
  $(".tab-bar .tab-item").click(function () {
    $(this).addClass("active").siblings().removeClass("active")
    var idx = $(this).index()
    $(".main").eq(idx).addClass("selected").siblings().removeClass("selected")
  })

  // 点击播放视频
  $("#video-btn").click(function () {
    var video = $("#ckplayer")[0]
    $('.videobox').show()
    video.play()
    $(".videoboxClose").show()
    $("#modalVideo").show()
    $(".videoboxMask").show()
  })
  // 暂停播放视频
  $(".videoboxClose").click(function () {
    var video = $("#ckplayer")[0]
    $('.videobox').hide()
    video.pause()
    $(".videoboxClose").hide()
    $("#modalVideo").hide()
    $(".videoboxMask").hide()
  })

  // 优秀病例列表点赞
  $("#records-list").on('click', '.zan-info', function () {
    var $zan = $(this).children('[data-zan]')
    var $num = $(this).children('.num')
    var id = $(this).data('id')

    if ($zan.data('zan')) {
      toggleZan($zan, $num, id, true)
    } else {
      toggleZan($zan, $num, id, false)
    }
    return false
  })
  function toggleZan($zan, $num, recordId, flag) {
    count = $num.data('count')
    if (flag) {
      $zan.removeClass('icon-zan2')
      $zan.addClass('icon-zan1')
      count += 1
    } else {
      $zan.removeClass('icon-zan1')
      $zan.addClass('icon-zan2')
      count -= 1
    }
    $num.html(count)
    $num.data('count', count)
    $zan.data('zan', !flag)
    var data = {
      recordId: recordId,
      isZan: flag
    }
    console.log(data)
    // 发送请求
    //$.ajax({
    //  url: '/',
    //  type: 'POST',
    //  data: data,
    //  success: function (res) {
    //
    //  }
    //})
  }

  // 获取菜单
  function getMenu() {
    var data = []
    if (!$.cookie('userToken') || $.cookie('userToken') === 'null') {
      $("#role-menu").html('<a href="login.html">登录/注册<a/>')
      return
    } else {
      var role = JSON.parse($.cookie('userToken')).role
      if (role === 1) { // 医生
        data = docMens
      } else if (role === 2) { // 药代
        data = medicineRepMenus
      }
    }
    var html = template('user-menu-tmpl', data)
    $("#role-menu").html(html)
  }

  // 获取优秀病例列表
  function getRecords() {
    var data = [{
      id: '1111',
      title: 'xxxxxxxxx病例',
      docotor: 'xxx医院xxx医生',
      isZan: false,
      zanCount: 12,
      commentCount: 135
    }, {
      id: '222',
      title: 'xxxxxxxxx病例',
      docotor: 'xxx医院xxx医生',
      isZan: true,
      zanCount: 15,
      commentCount: 68
    }]
    var html = template('good-records-tmpl', data)
    $("#records-list").html(html)
  }

})